import 'dart:convert';

import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';

/*
退出圈地
移动点
撤销；
定位，发endMove
scale：
接收消息
 */
class DrawLandWebSocketListener {
  static List<void Function(dynamic message)> messageListener = [];
  static Null Function(dynamic message) drawLandWebsocketListener =
      (dynamic message) {
    print("我在SDK里面收到了消息");
    for (var element in messageListener) {
      element.call(message);
    }
  };
  static late Function(String smg) call;

  static String websocket_userId = "";
  static String websocket_sessionId = "";

  static String websocket_staffuserId = "";
  static void send(String msg) {
    call.call(msg);
  }

  static void startDraw() {
    if (websocket_userId.isEmpty ||
        websocket_sessionId.isEmpty ||
        websocket_staffuserId.isEmpty) {
      return;
    }
    send(jsonEncode({
      "code": "21025",
      "userId": websocket_userId,
      "staffUserId": websocket_staffuserId,
      "videoSessionId": websocket_sessionId,
      "channel": "1",
      "data": {
        "type": "begin",
      }
    }));
  }

  static void exitDraw() {
    if (websocket_userId.isEmpty ||
        websocket_sessionId.isEmpty ||
        websocket_staffuserId.isEmpty) {
      return;
    }
    send(jsonEncode({
      "code": "21025",
      "userId": websocket_userId,
      "staffUserId": websocket_staffuserId,
      "videoSessionId": websocket_sessionId,
      "channel": "1",
      "data": {
        "type": "save",
      }
    }));
  }

  static void addPoint(List<Position> points) {
    if (websocket_userId.isEmpty ||
        websocket_sessionId.isEmpty ||
        websocket_staffuserId.isEmpty) {
      return;
    }
    send(jsonEncode({
      "code": "21025",
      "userId": websocket_userId,
      "staffUserId": websocket_staffuserId,
      "videoSessionId": websocket_sessionId,
      "channel": "1",
      "data": {
        "type": "block",
        "points":
            points.map((e) => {"latitude": e.lat, "longitude": e.lng}).toList()
      }
    }));
  }

  static void endMove(ScreenCoordinate position) {
    if (websocket_userId.isEmpty ||
        websocket_sessionId.isEmpty ||
        websocket_staffuserId.isEmpty) {
      print("没有数据,停止发送同步勾画信息");
      return;
    }
    send(jsonEncode({
      "code": "21025",
      "userId": websocket_userId,
      "staffUserId": websocket_staffuserId,
      "videoSessionId": websocket_sessionId,
      "channel": "1",
      "data": {
        "type": "endMove",
        "latitude": position.x,
        "longitude": position.y,
      }
    }));
  }

  static void addListener(Null Function(dynamic message) param0) {
    messageListener.add(param0);
  }

  static void scaleChange(int zoom) {
    if (websocket_userId.isEmpty ||
        websocket_sessionId.isEmpty ||
        websocket_staffuserId.isEmpty) {
      return;
    }
    send(jsonEncode({
      "code": "21025",
      "userId": websocket_userId,
      "staffUserId": websocket_staffuserId,
      "videoSessionId": websocket_sessionId,
      "channel": "1",
      "data": {
        "type": "scale",
        "zoom": zoom,
      }
    }));
  }
}
